﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using EdBusTracker.V2;
using System.IO.IsolatedStorage;
using System.IO;

namespace EdBusTracker
{
    public partial class Page1 : PhoneApplicationPage
    {
        BusStops bus_stops;
        public Page1()
        {
            InitializeComponent();
            bus_stops = (App.Current as App).busStops;
            loadFavs();
            //busStops = new BusStops();
        }

        private void buttonLocation_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/V2/LocationPage.xaml", UriKind.Relative));
        }

        private void buttonFavorites_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/V2/Favs.xaml", UriKind.Relative));
        }

        public void loadFavs()
        {
            try
            {
                using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
                using (var file = appStorage.OpenFile("favs.txt", FileMode.Open))
                {
                    StreamReader reader = new StreamReader(file);
                    String sc = null;
                    while ((sc = reader.ReadLine()) != null)
                    {
                        String sn = bus_stops.getStopName(sc);
                        bus_stops.getFavs().Add(sn, sc);
                    }
                }
            }
            catch
            {
                createFavs();
            }
        }
        public void createFavs()
        {
            var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
            var file = appStorage.OpenFile("favs.txt", FileMode.Create);
        }

        private void buttonAbout_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/V2/About.xaml", UriKind.Relative));
        }

        private void buttonTwitter_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/V2/Twitter.xaml", UriKind.Relative));
        }

    }
}